<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/iconfont4.css">
		<script src="js/mui.min.js"></script>
		<style type="text/css">
			#bcid {
				width: 100%;
				height: 100%;
				position: absolute;
				background: #000000;
			}
			
			html,
			body,
			div {
				height: 100%;
				width: 100%;
			}
			
			.fbt {
				color: #0E76E1;
				width: 50%;
				background-color: #ffffff;
				float: left;
				line-height: 50px;
				text-align: center;
			}
			
			.iconfont {
				padding: 12px;
			}
		</style>
	</head>

	<body>
		<div class="mui-bar mui-bar-nav" style="background-color: #ffffff;">
			<a href="organize.html" onclick="back()" class=" mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title" style="color: #0E76E1;">二维码扫描</h1>
			<span class="iconfont icon-shoudiantong   mui-pull-right " id="turnTheLight"></span>
		</div>

		<center>
			<div id="bcid" >
				<!--盛放扫描控件的div-->
			</div>
		</center>

		<div class="mui-bar mui-bar-footer" style="padding: 0px;">
			<div class="fbt" onclick="scanPicture();">从相册选择二维码</div>
			<div class="fbt  " onclick="back()">
				<a href="organize.html">取　 消</a>
			</div>
		</div>

		<script type="text/javascript">
			var id=localStorage.getItem("employeeid");
			scan = null; //扫描对象  
			mui.plusReady(function() {
				mui.init();
				startRecognize();
			});

			function startRecognize() {
				try {
					var filter;
					//自定义的扫描控件样式  
					var styles = {
						frameColor: "#29E52C",
						scanbarColor: "#29E52C",
						background: ""
					}
					//扫描控件构造  
					scan = new plus.barcode.Barcode('bcid', filter, styles);
					scan.onmarked = onmarked;
					scan.onerror = onerror;
					scan.start();

					//打开关闭闪光灯处理  
					var flag = false;
					document.getElementById("turnTheLight").addEventListener('tap', function() {
						if(flag == false) {
							scan.setFlash(true);
							flag = true;
						} else {
							scan.setFlash(false);
							flag = false;
						}
					});
				} catch(e) {
					alert("出现错误啦:\n" + e);
				}

			};

			function onerror(e) {
				alert(e);
			};

			function back() { //关闭控件
				scan.close();
				
				
			};
			
			function joinCom(){
				//window.location.href="qiye-list.html";
				var network = true;
				if(mui.os.plus) {
					mui.plusReady(function() {
						if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
							network = false;
						}
					});
				}
				if(network) {
						console.log('网络正常,正在发送请求...');
						mui.ajax({
                 				async:true,
     							type:'post',
                				url:'http://113.71.26.27:8080/tpl/phone/joinCom',
                				data:{
                					id:id
                				},
                			    success: function(data){
                			    	if(data=="ok"){
                			    		alert("加入成功,请重新登录");
                			    		plus.webview.currentWebview().hide();
				
							mui.openWindow({
												url: 'setting.html',
												id: 'setting.html',
											});
							
								
								
								
								
                			    	}else{
                			    		mui.toast("更改失败，请重新扫描！");
                			    	}
                			    }
                			  })
					}else {
						mui.toast("当前网络不给力，请稍后再试...");
					}
			}

			function onmarked(type, result) {
				var text = '';
				switch(type) {
					case plus.barcode.QR:
						var btnArray = ['否', '是'];
						mui.confirm('是否加入该企业？', '', btnArray, function(e) {
							if(e.index == 1) {
								back();
								joinCom();
								
							}else{
								window.location.href="saoYisao.html";
							}
						});
						break;
					case plus.barcode.EAN13:
						text = 'EAN13: ';
						break;
					case plus.barcode.EAN8:
						text = 'EAN8: ';
						break;
					default:
						text = '其它' + text;
						break;
				}
				

			};
			
		
			// 从相册中选择二维码图片   
			function scanPicture() {
				scan.onmarked = onmarked;
				plus.gallery.pick(function(path) {
					plus.barcode.scan(path, onmarked, function(error) {
						//back();
						plus.nativeUI.alert("无法识别 " );
					});
				}, function(err) {
					plus.nativeUI.alert("失败: " + err.message);

				});
			}
			
			/**
		 * 重写返回事件
		 */
		mui.back = function(){
			scan.close();
			window.location.href = "organize.html";
		}
		</script>
	</body>

</html>